package com.jcompressor.model.config;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jcompressor.assertion.AssertArgument;

public class Configuration implements Serializable {
	
	private static final long serialVersionUID = -7466310072076755116L;
	
	private List<Property> properties;
	private Build build;
	private List<Resource> resources;
	
	public Configuration() {
		this.properties = new ArrayList<Property>();
		this.resources = new ArrayList<Resource>();
	}
	
	public List<Property> getProperties() {
		return this.properties;
	}
	
	public Property getProperty(final String key) {
		AssertArgument.isNotBlank(key, "key");
		
		for (final Property property : this.properties) {
			if (StringUtils.equals(key, property.getKey())) {
				return property;
			}
		}
		
		return null;
	}
	
	public Build getBuild() {
		return this.build;
	}
	
	public List<Resource> getResources() {
		return this.resources;
	}
	
	public Resource getResource(final String id) {
		AssertArgument.isNotBlank(id, "id");
		
		for (final Resource resource : this.resources) {
			if (StringUtils.equals(id, resource.getId())) {
				return resource;
			}
		}
		
		return null;
	}
	
	public void setProperties(final List<Property> properties) {
		this.properties = properties;
	}
	
	public void setBuild(final Build build) {
		this.build = build;
	}
	
	public void setResources(final List<Resource> resources) {
		this.resources = resources;
	}
	
	public void addProperty(final Property property) {
		this.properties.add(property);
	}
	
	public void addResource(final Resource resource) {
		this.resources.add(resource);
	}
	
	public boolean containsProperty(final String key) {
		AssertArgument.isNotBlank(key, "key");
		
		for (final Property property : this.properties) {
			if (StringUtils.equals(key, property.getKey())) {
				return true;
			}
		}
		
		return false;
	}
	
}